﻿namespace TextFiles
{
    using System;
    using System.Collections.Generic;
    using System.IO;

    class RemoveInAFileAllWordsFromAFile
    {
        // 1.2.12.12. Write a program that removes from a text file all words listed in given another text file.
        // Handle all possible exceptions in your methods.

        static void Main()
        {
            try
            {
                var inputWords = new StreamReader(@"../../inputWords.txt");
                var inputTextForEdit = new StreamReader(@"../../inputWords.txt");
                var output = new StreamWriter(@"../../output.txt");
                using (inputWords)
                {
                    var wordsExamples = new List<string>();
                    var wordsLine = inputWords.ReadLine();
                    while (wordsLine != null)
                    {
                        string[] tempWords = wordsLine.Split(' ', '.', '!', ',', '?', '-');
                        foreach (var word in tempWords)
                        {
                            wordsExamples.Add(word);
                        }

                        wordsLine = inputWords.ReadLine();
                    }
                    using (inputTextForEdit)
                    {
                        using (output)
                        {
                            string line = inputTextForEdit.ReadLine();
                            while (line != null)
                            {
                                foreach (var word in wordsExamples)
                                {
                                    line = line.Replace(" " + word + " ", "");
                                }

                                output.WriteLine(line);
                                line = inputTextForEdit.ReadLine();
                            }
                        }
                    }
                } 
            }
            catch (FileNotFoundException e)
            {
                Console.WriteLine(e.Message);
            }
            catch (DirectoryNotFoundException e)
            {
                Console.WriteLine(e.Message);
            }
            catch (IOException e)
            {
                Console.WriteLine(e.Message);
            }
            catch (UnauthorizedAccessException e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
}